//
//  RNOLNewHandInvestViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/17.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLNewHandInvestViewController.h"
#import "RNOLNewBidHeader.h"
#import "RNOLBidRecordListViewController.h"
#import "RNOLBuyInvestViewController.h"
#import "RNOLInvestDetailListCell.h"

@interface RNOLNewHandInvestViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSDictionary *dataDict;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) CountDown *countDownObj;
@property (nonatomic,copy) NSString *timeStr;
@property (nonatomic,strong) RNOLNewBidHeader *header;

@end

@implementation RNOLNewHandInvestViewController

- (NSMutableArray *)dataSource
{
    if (_dataSource==nil) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}

- (RNOLNewBidHeader *)header{
    if (!_header) {
        _header = [RNOLNewBidHeader FromXIB];
        _header.frame = CGRectMake(0, 0, KWIDTH , 350);
    }
    return _header;
}

- (CountDown *)countDownObj{
    if (!_countDownObj) {
        _countDownObj = [[CountDown alloc] init];
    }
    return _countDownObj;
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , KHIGHT - kNavBarHeight - 50) style:UITableViewStyleGrouped];
        _tableView.bounces = NO;
        _tableView.backgroundColor = kBgColor;
        adjustsScrollViewInsets_NO(_tableView, self);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView regsiterCellWithCellClass:[RNOLInvestDetailListCell class] isNib:YES];
        _tableView.height -= kBottomHeight;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview: _tableView];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBarTitleViewWithText:@"投资理财"];
    self.view.backgroundColor = kBgColor;
    [self setup];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [RNOLLoginTool setNavgationBarNeedAnimation:YES];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)setup{
    self.timeStr = @"66666";
    self.dataDict = @{
                      @"0" : @[@"项目名称",@"项目类型",@"起息时间",@"剩余时间",@"协       议",@"温馨提示"],
                      @"1" : @[@"产品详情",@"投标记录"]
                      };
    self.dataSource = [@[@"20180523150666SZ",@"车抵贷",@"满标后当日起息",self.timeStr,@"《借款协议》",@"新手专享3次投资,累计限额5万元"] mutableCopy];
    self.tableView.tableHeaderView = self.header;
    [self.tableView reloadData];
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, KHIGHT - kNavBarHeight - 50, KWIDTH , 50)];
    bottomView.y -= kBottomHeight;
    bottomView.backgroundColor = KWhiteColor;
    [self.view addSubview: bottomView];
    
    UIButton *calButton = [UIButton buttonWithType:UIButtonTypeCustom];
    calButton.backgroundColor = KWhiteColor;
    [calButton setImage:[UIImage imageNamed:@"rnol_calcutor"] forState:UIControlStateNormal];
    calButton.frame = CGRectMake(0, 0, 50 , 50);
    [calButton addTarget:self action:@selector(alertCaltorAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview: calButton];
    

    UIButton *immediatelyLoanBtn = [RNOLUnitTool createRNOLStyleBlueButtonWithButtonTitle:@"立即借款"];
    immediatelyLoanBtn.x = 50;
    immediatelyLoanBtn.width = KWIDTH - 50;
    immediatelyLoanBtn.height = 50;
    immediatelyLoanBtn.y = 0;
    [immediatelyLoanBtn addTarget:self action:@selector(immediatelyLoanAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview: immediatelyLoanBtn];
    
    @weakify(self);
    [self.countDownObj countDownWithStratDate:[NSDate date] finishDate: [[NSDate date] dateByAddingTimeInterval: 86400*6] completeBlock:^(NSInteger day, NSInteger hour, NSInteger minute, NSInteger second) {
        @strongify(self);
        self.timeStr = [NSString stringWithFormat:@"%ld天%ld时%ld分%ld秒",day,hour,minute,second];
        dispatch_main_async_safe(^{
            [self.dataSource replaceObjectAtIndex:3 withObject: self.timeStr];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        });
    }];
}


#pragma mark- 弹出计算器
- (void)alertCaltorAction:(UIButton *)button{
    
    
}

#pragma mark- 立即借款
- (void)immediatelyLoanAction{
    [self pushPageWithClass:[RNOLBuyInvestViewController class]];
}

#pragma mark -  UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataDict.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *items = self.dataDict[@(section).stringValue];
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *items = self.dataDict[@(indexPath.section).stringValue];
    RNOLInvestDetailListCell *cell = [tableView dequeueReusableCellWithIdentifier:RNOLInvestDetailListCell.cellReuseID];
    cell.valueLabel.text = indexPath.section? @"" : self.dataSource[indexPath.row];
    cell.nameLabel.text = items[indexPath.row];
    cell.bottomLineView.hidden = cell.rightArrowImageView.hidden = !indexPath.section;
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section? 44 : 35;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [RNOLUnitTool createViewWithBackgroundColor:kBgColor];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *items = self.dataDict[@(indexPath.section).stringValue];
    NSString *title = items[indexPath.row];
    if (kCompareTwoString(title,@"产品详情")) {
        
    }else if (kCompareTwoString(title, @"投标记录")){
        [self pushPageWithClass:[RNOLBidRecordListViewController class]];
    }else if (kCompareTwoString(title, @"协       议")){
        [self customAlertWithTitle:@"协议" cancelAction:^{
            
        } comfirmAction:^{
            
        }];
    }

}

@end
